/**
 * Live GPS Tracking Application
 * Satellite information widget
 *
 * Copyright 2009, Ivar Clemens
 */

package nl.ivarclemens.gpstrack.ui;

//import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Hashtable;
import java.util.Enumeration;

import nl.ivarclemens.gpstrack.nmea.SatelliteEvent;

public class SatelliteWidget extends CustomItem implements SatelliteEvent {

    public class Satellite
    {
        public int azimuth, elevation, snr;
    }

    public SatelliteWidget()
    {
        super("Satellite view");
    }

    private Hashtable satellites = new Hashtable();

    private Graphics g;

    private int centreX;
    private int centreY;
    private int radius;

    private int getMaxSize(int w, int h)
    {       
        if(w > h) {
            return h;
        } else {
            return w;
        }
    }

    private void fillCircle(int x, int y, int radius)
    {
        g.fillArc(x - radius, y - radius,
                radius * 2, radius * 2, 0, 360);
    }

    private void drawCircle(int x, int y, int radius)
    {
        g.drawArc(x - radius, y - radius,
                radius * 2, radius * 2, 0, 360);
    }

    private void drawSatellite(int az, int el, int snr)
    {
        int r = ((90 - el) * radius) / 90;
        int x = (int) (Math.sin(az / 180.0 * Math.PI) * (double) r);
        int y = (int) -(Math.cos(az / 180.0 * Math.PI) * (double) r);

        int size = (int) radius / 15;

        if(snr == 0)
            g.setColor(255, 128, 128);
        else
            g.setColor(128, 255, 128);

        this.fillCircle(centreX + x, centreY + y, size);

        g.setColor(255, 255, 255);
        g.fillRect(centreX + x - size - 4, centreY + y - size, 4, 2 * size);
        g.fillRect(centreX + x + size, centreY + y - size, 4, 2 * size);

        g.setColor(0, 0, 0);
        this.drawCircle(centreX + x, centreY + y, size);

        g.drawRect(centreX + x - size - 4, centreY + y - size, 4, 2 * size);
        g.drawRect(centreX + x + size, centreY + y - size, 4, 2 * size);
    }

    public void paint(Graphics g, int w, int h)
    {
        this.g = g;

        // White background

        //g.setColor(255, 255, 255);
        //g.fillRect(0, 0, w, h);

        // Store parameters for satellite placement
        int margin = 10;

        centreX = w / 2;
        centreY = h / 2;
        radius = this.getMaxSize(w, h) / 2 - margin;

        // Rings
        g.setColor(255, 255, 255);
        //this.fillCircle(centreX, centreY, radius);
        //this.fillCircle(centreX, centreY, radius / 2);

        g.setColor(0, 0, 0);
        this.drawCircle(centreX, centreY, radius);
        this.drawCircle(centreX, centreY, radius / 2);

        // Satellites
        Enumeration em = satellites.elements();

        while(em.hasMoreElements()) {
            Satellite sat = (Satellite) em.nextElement();
            this.drawSatellite(sat.azimuth, sat.elevation, sat.snr);
        }
    }

    public int getMinContentWidth()
    {
        return 51;
    }

    public int getMinContentHeight()
    {
        return 51;
    }

    public int getPrefContentWidth(int width)
    {
        return 1;
    }

    public int getPrefContentHeight(int height)
    {
        return 1;
    }

    /**
     * Satellite interface
     */

    public void satUpdateBegins()
    {
        satellites.clear();
    }

    public void satUpdateComplete()
    {
        //if(this.isShown())
        //this.
        this.repaint();
    }

    public void satUpdate(int prn, int azimuth, int elevation, int snr)
    {
        Satellite satellite = new Satellite();
        satellite.azimuth = azimuth;
        satellite.elevation = elevation;
        satellite.snr = snr;

        satellites.put(Integer.toString(prn), satellite);
    }
}
